package net.bithappens.hnqe.impl.rest.provider;

import java.lang.reflect.Type;

import net.bithappens.hnqe.graph.GraphNode;
import net.bithappens.hnqe.graph.GraphValue;

import riotcmd.json;

import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;

public class GraphRelationMemberJsonAddapter implements JsonSerializer<Object>, JsonDeserializer<Object> {

	
	@Override
	public JsonElement serialize(Object object, Type type, JsonSerializationContext jsonSerializationContext) {
		
		JsonElement jsonEle = jsonSerializationContext.serialize(object, object.getClass());

		return jsonEle;

	}
	
	
	@Override
	public Object deserialize(JsonElement jsonElement, Type type, 
		JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
	
		JsonObject jsonObj = jsonElement.getAsJsonObject();

		if(jsonObj.get("type") != null){
			return jsonDeserializationContext.deserialize(jsonElement, GraphNode.class);
		} else {
			return jsonDeserializationContext.deserialize(jsonElement, GraphValue.class);
		}

	}
	
}
